define(['moment'], function(moment) {
  var self = {};

  // format a date to a light object
  function format(date) {
    var formattedDate = _(['getFullYear', 'getMonth', 'getDate', 'getDay']).map(function(foo) {
      return [
        foo.match(/[A-Z][a-z]*$/)[0].toLowerCase(),
        date[foo]()
      ];
    }).fromPairs().value();
    formattedDate.interval = moment(date).unix();
    return formattedDate;
  }
  // how much days does the current month of the date has
  function numberOfDays(formattedDate) {
    date = new Date(formattedDate.year, formattedDate.month + 1, 0);
    return Number(moment(date).format('D'));
  }
  function todayInLastMonth(formattedToday) {
    return new Date(formattedToday.year, formattedToday.month - 1, formattedToday.date);
  }
  function todayInNextMonth(formattedToday) {
    return new Date(formattedToday.year, formattedToday.month + 1, formattedToday.date);
  }
  function beautify(date) {
    var formattedDate = format(date)
    var days = [
      '周一',
      '周二',
      '周三',
      '周四',
      '周五',
      '周六',
      '周日'
    ];
    formattedDate.day = days[formattedDate.day];
    formattedDate.date = formattedDate.date.toString() + '日';
    formattedDate.month = (formattedDate.month + 1).toString() + '月';
    return formattedDate;
  }
  // get dates in two month range
  self.get = function() {
    var today = format(new Date());
    var dates = new Array();
    // dates in last month
    var date = format(todayInLastMonth(today));
    var lastDayInLastMonth = numberOfDays(date);
    for (var i = today.date; i <= lastDayInLastMonth; ++i) {
      dates.push(beautify(new Date(date.year, date.month, i)));
    }
    // dates in current month
    date = today;
    var lastDayInThisMonth = numberOfDays(date);
    for (var i = 1; i <= lastDayInThisMonth; ++i) {
      dates.push(beautify(new Date(date.year, date.month, i)));
    }
    // dates in next month
    date = format(todayInNextMonth(today));
    for (var i = 1; i <= date.date; ++i) {
      dates.push(beautify(new Date(date.year, date.month, i)));
    }
    return dates;
  }

  function isEqual(obj1, obj2) {
    return obj1.year == obj2.year && obj1.month == obj2.month && obj1.date == obj2.date;
  }

  self.getIndex = function(dates, date) {
    date = beautify(date);
    var result = -1;
    dates.forEach(function(value, index, array) {
      if (isEqual(date, value)) {
        result = index;
      }
    })
    return result;
  }
  return self;
})
